package com.daocao.ds.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.support.v7.widget.RecyclerView;
import android.text.Spanned;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.daocao.ds.R;
import com.daocao.ds.activity.MyOrderDetailActivity;
import com.daocao.ds.activity.OrderDetailActivity;
import com.daocao.ds.api.model.ManagerListModel;
import com.daocao.ds.util.Spans;
import com.daocao.ds.util.TimeUtil;
import com.daocao.ds.view.StarNumberView;

import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by xsooy-pc on 2017/10/23.
 */

public class MyOrderAdapter extends ItemAdapter<ManagerListModel.DatasBean, MyOrderAdapter.DefaultHolder> {


    private Context context;

    public MyOrderAdapter(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public DefaultHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DefaultHolder(inflate(R.layout.item_my_order, parent));
    }

    @Override
    public void onBindViewHolder(DefaultHolder holder, int position) {
        ManagerListModel.DatasBean item = getItem(position);
        holder.txtRealname.setText(item.real_name);
        //借款状态： 1 已回款、2 使用中、3 回款期、4 已逾期
        switch (item.deal_status) {
            case "1":
                holder.btnStatus.setBackgroundResource(R.mipmap.img_status_yihuikuan);
                holder.btnStatus.setText("已回款");
                break;
            case "2":
                holder.btnStatus.setBackgroundResource(R.mipmap.img_home_status_select);
                holder.btnStatus.setText("使用中");
                break;
            case "3":
                holder.btnStatus.setBackgroundResource(R.mipmap.img_status_huikuanqi);
                holder.btnStatus.setText("回款期");
                break;
            case "4":
                holder.btnStatus.setBackgroundResource(R.mipmap.img_status_yuqi);
                holder.btnStatus.setText("已逾期");
                break;
        }
        holder.txtName.setText(item.company_name);
        if (item.number.equals("0")) {
            holder.txtNumber.setText("首次");
            holder.txtNumber.setBackgroundResource(R.mipmap.img_home_number_select);
        } else {
            holder.txtNumber.setText(item.number + "次");
            holder.txtNumber.setBackgroundResource(R.mipmap.img_home_number_unselect);
        }
        holder.txtTime.setText(TimeUtil.format(new Date(Long.valueOf(item.created_at) * 1000)));

        holder.txtMoney.setText(getInfoText("金额：", item.borrow_amount + ""));
        holder.txtDate.setText(getInfoText("期限：", item.limit_days + "天"));
        holder.txtRate.setText(getInfoText("年化利率：", item.rate + "%"));

        holder.imgStar.setStar(Integer.valueOf(item.level_id));

        holder.txtBorrowDate.setText("出借日期：" + item.start_date);
        holder.txtReturnDate.setText("回款日期：" + item.repay_date);
        holder.layoutTotal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, MyOrderDetailActivity.class);
                intent.putExtra("id", item.id);
                context.startActivity(intent);
            }
        });
    }

    private Spanned getInfoText(String one, String two) {
        return Spans.with(context).font(one, context.getResources().getDimensionPixelSize(R.dimen.x24), 0xff767676).
                font(two, context.getResources().getDimensionPixelSize(R.dimen.x32), 0xff0072FF).style(Typeface.BOLD).build();
    }

    class DefaultHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.txt_name)
        TextView txtName;
        @BindView(R.id.layout_name)
        LinearLayout layoutName;
        @BindView(R.id.btn_status)
        TextView btnStatus;
        @BindView(R.id.txt_realname)
        TextView txtRealname;
        @BindView(R.id.img_star)
        StarNumberView imgStar;
        @BindView(R.id.txt_number)
        TextView txtNumber;
        @BindView(R.id.txt_time)
        TextView txtTime;
        @BindView(R.id.txt_money)
        TextView txtMoney;
        @BindView(R.id.txt_date)
        TextView txtDate;
        @BindView(R.id.txt_rate)
        TextView txtRate;
        @BindView(R.id.txt_borrow_date)
        TextView txtBorrowDate;
        @BindView(R.id.txt_return_date)
        TextView txtReturnDate;
        @BindView(R.id.layout_total)
        LinearLayout layoutTotal;
        public DefaultHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    private OnChooseListener onChooseListener;

    public void setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
    }

    public interface OnChooseListener {
        void onSuccess();
    }
}
